//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------
#pragma once

#ifndef _D2_WINDOW_CONTAINER_H
#define _D2_WINDOW_CONTAINER_H

#include "..\..\DisplayObjectContainer.h"
#include "..\..\..\geom\Point2f.h"
#include "..\..\..\geom\Rect.h"

#include <string>


using namespace D2;

class WindowContainer : public D2::DisplayObjectContainer {
public:
	WindowContainer(int x, int y, int width, int height);
	virtual ~WindowContainer(void);

protected:
	std::string _label;

public:
	bool _draggable;
	bool _resizable; //on resizeable = true -> an _outlineWidth must be set higher than 0.

	int _resizableRaster; //defnies a grid for the resizable movement.
	int _draggableRaster;  //defnies a grid for the draggable movement.

	int _outlineWidth;

	int _minWidth; //default -1
    int _minHeight; //default -1
    int _maxWidth; //default -1
    int _maxHeight; //default -1

private:
	DisplayObjectContainer* displayObjectContainer_inner;

	Point2f* _tempLocalDragPoint;
	Rect* _boundingRect;

	int _currentOverSide;
    int _currentSelectedSide;

public:
	void SetLabel(std::string label);
	void SetBounds(float x, float y, float width, float height);
	void RemoveBounds();

private:
	void Initialize();

	DisplayObjectContainer* _button_container;

	DisplayObjectContainer* CreateButtons();
	void UpdateButtonPosition();

public:
	void WindowContainer::ShowButtons(bool enable);

public:
	//@IMouseListener
	virtual void OnMouseMove(const MouseEvent &e);

	//@IInput
	//virtual void OnMouseOver(const MouseEvent &e);
	virtual void OnMouseOut(const MouseEvent &e);
	virtual void OnFocusLost(const MouseEvent &e);
	
	virtual void OnMouseDown0(const MouseEvent &e);
	virtual void OnMouseUp0(const MouseEvent &e);
	virtual void OnMouseLeave0(const MouseEvent &e);
	virtual void OnMouseRaise0(const MouseEvent &e);
	
	virtual void OnMouseDrag0(const MouseEvent &e);

	virtual void OnMouseDown1(const MouseEvent &e);
	virtual void OnMouseUp1(const MouseEvent &e);
	virtual void OnMouseRaise1(const MouseEvent &e);
	virtual void OnMouseLeave1(const MouseEvent &e);

//private:
	//Point _currentMousePos;

////////////////////////////VISUALISATION//////////////////////////

	virtual bool Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx = 0);

	virtual DisplayObject* addChild(DisplayObject* displayObject);
	
	virtual DisplayObject* getChildAt(unsigned int index);
	virtual int getNumChildren();
	virtual void removeAllChildren(bool recursive = false, bool del = false);

private:
	void DrawWindowBar(Direct2DContext* ctx);
	void DrawWindow(Direct2DContext* ctx);

private:
	void drag(const MouseEvent &e);
	void resize(const MouseEvent &e);

//////////////////////////////
private:
	bool HitTestOutline(float x, float y);
	int GetHittenArea(float x, float y);

	void RepositionByBounds();

//////////////////////////////

public:
	void Highlight(bool enable);

protected:
	bool _highlight;
	bool _fixX;
	bool _fixY;

public:
	void FixX(bool fix);
	void FixY(bool fix);

private:
	float _r_stroke, _g_stroke, _b_stroke;
	float _r_fill, _g_fill, _b_fill;

public:
	void SetFgColor(float r, float g, float b);
	void SetBgColor(float r, float g, float b);

};

#endif